import {PropsWithoutRef, RefAttributes, ForwardRefExoticComponent, FC, ReactNode} from "react";
import type { ModalProps, PaginationProps} from "antd";
import {SkTableProps} from "./Content/SkTable";

export type OnError = (errorMsg: string) => void;

export interface FetchListParams {
    page: number;
    pageSize: number;
    filter?: {[index: string]: any};
    orders?: {field: string, orderMethod: string}[]
}

export interface DataAction {
    fetchList: (params: FetchListParams) => Promise<any>;                   // 获取数据列表
    getItem?: (rowKey: string | number) => Promise<any>;                    // 依据rowKey获取数据
    getCreateDefaultValue?: () => Promise<any>;                             // 获取创建的默认值
    createItem?: (params: any) => Promise<any>;                             // 创建数据
    updateItem?: (rowKey: string | number, params: any) => Promise<any>;    // 更新数据
    deleteItems?: (selectedRowKeys: string[]) => Promise<any>;       // 删除数据列表
    copyItems?: (selectedRowKeys: string[]) => Promise<any>;         // 复制数据列表
    [index: string]: (...args:any[]) => Promise<any>;
}


export interface CreateOrEditActionRef {
    getFormData: <T = any>() => Promise<T>
}

export interface FilterActionRef {
    getFormData: <T = any>() => Promise<T>
}

export type ExtendOperatorComponent = FC<{
    selectedRowKeys?: string[],
    selectedRows?: any[],
    clearSelected?: () => void,
    doFetchList?: () => void;
    filter?: any,
    label?: string,
}>;

export interface FilterItem{
    title: string,
    name: string,
    gridSpan: number,
    component: ReactNode,
    defaultValue?: any,
}

//-----------------------------------------header---------------------------------------

export interface IOuterCreateOrEditProps {
    modalProps?: ModalProps;
    getModalTitle?: (rowKey?: number| string) => string;
    // schemaFormProps?: ProJsonSchemaFormProps;
    // formatFormSchema?: (rowKey: string | number) => ISchema;
    // asyncFormatFormSchema?: (rowKey: string | number) => Promise<ISchema>;
    CreateOrEditContent?: ForwardRefExoticComponent<PropsWithoutRef<{rowKey: string | number, values: any}> & RefAttributes<CreateOrEditActionRef>>;
}

type InnerOperatorType = "create"|"edit"|"delete"|"copy";

export interface IOuterOperatorProps {
    btnList?: {
        innerType?: InnerOperatorType | string;
        label?: string;
        onClick?:(selectedRowKeys: string[]) => void;
        ExtendComponent?: ExtendOperatorComponent;
        isDisableFn?: (data: any[]) => boolean;
        authKey?: string;
    }[];
    createOrEditProps?: IOuterCreateOrEditProps
}

export interface IOuterFilterProps {
    justify?: "left" | "right"; // 水平对齐方式
    maxWidth?: string;
    moreFilterWidth?: string;
    // schemaFormProps?: ProJsonSchemaFormProps;
    // moreSchemaFormProps?: ProJsonSchemaFormProps;
    FilterContent?: ForwardRefExoticComponent<PropsWithoutRef<{ doSearch: Function }> & RefAttributes<FilterActionRef>>,
    MoreFilterContent?: ForwardRefExoticComponent<PropsWithoutRef<{ doSearch: Function }> & RefAttributes<FilterActionRef>>,
    searchBtnVisible?: boolean,     // 搜索按钮是否可见
    filterItems?:FilterItem[],
}

export interface IOuterHeaderProps {
    isShow?: boolean;
    operatorProps?: IOuterOperatorProps;
    filterProps?:IOuterFilterProps
}


//----------------------------------------content-------------------------------------------
interface IContentProps {
    isShow?: boolean;
    isShowRowSelection?: boolean;
    tableProps: SkTableProps;
    Empty?: FC;
}

//----------------------------------------footer-------------------------------------------
interface IFooterProps {
    isShow?: boolean;
    pagination?: PaginationProps;
}

export interface ITheme{
    headerHeight?: number;
    footerHeight?: number;
    headerBgColor?: string;
    headerBoxShadow?: string;
    footerBgColor?: string;
    footerBoxShadow?: string;
}

// 对外的表格props
export interface IProTableProps {
    rowKey?: string|number;         // 指定rowKey中的字段
    fetchListDeps?:any[];           // 依赖改变引起触发fetchList获取数据
    onError?: OnError;              // 监听操作中的错误信息
    dataAction: DataAction;         // 操作数据的API
    theme?:ITheme;
    headerProps?: IOuterHeaderProps;
    contentProps: IContentProps;
    footerProps?: IFooterProps;
}

export interface IProTableRef {
    doFetchList: () => void;
    clearSelected: () => void;
}
